/*************************************************************************
	> File Name: oj-303_distance_aijtoakl.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2020年11月18日 星期三 19时20分49秒
 ************************************************************************/

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

struct node {
    int x, y, step;
};

int n, m, num[1005][1005];
int dir[4][2] {0, 1, 1, 0, 0, -1, -1, 0};
char mmap[1005][1005];

int main() {
    memset(num, -1, sizeof(num));
    queue<node> que;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> mmap[i][j];
            if (mmap[i][j] == '1') {
                //如果是1，设为起点，步数为1
                que.push((node){i, j, 0});
                num[i][j] = 0;
            }
        }
    }
    while (!que.empty()) {
        node temp = que.front();
        que.pop();
        for (int i = 0; i < 4; ++i) {
            int x = temp.x + dir[i][0];
            int y = temp.y + dir[i][1];
            //判断边界mmap为0是边界
            //判断是否访问过，-1为未访问
            if (mmap[x][y] != 0 && num[x][y] == -1) {
                num[x][y] = temp.step + 1;
                que.push((node){x, y, num[x][y]});
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            j == 1 || cout << " ";
            cout << num[i][j];
        }
        cout << endl;
    }
    return 0;
}
